home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C ++ / Applications / Conic Sections 0.9.2 / Sources / conic.cpp < prev    next >
Encoding:
Text File  |  1997-06-21  |  3.2 KB  |  167 lines  |  [TEXT/CWIE]

  1. //Copyright (c) 1997 Brian and Aidan Cully
  2. //All rights reserved
  3.  
  4. #include <stdio.h>
  5. #include <math.h>
  6. #include <stdlib.h>
  7. #include <ctype.h>
  8. #include <types.h>
  9. #include <QuickDraw.h>
  10. #include <Events.h>
  11. #include "conic.h"
  12.  
  13. extern p3d rotpoint(p3d,float);
  14.  
  15. TConic::TConic()
  16. {
  17.     mAConic = 0;
  18.     mConic = 0;
  19.     mIntersect = 0l;
  20. }
  21.  
  22. TConic::~TConic()
  23. {
  24.     if( mConic )
  25.         delete mConic;
  26.     if( mAConic )
  27.         delete mAConic;
  28.     if( mIntersect )
  29.         delete mIntersect;
  30. }
  31.  
  32. void TConic::Init()
  33. {
  34.     int surf;
  35.     p3d cpoint,bcpoint;
  36.  
  37.     mConic = new p3d[CONICPARTS+2];
  38.     mAConic = new p3d[CONICPARTS+2];
  39.     mIntersect = new int[CONICPARTS+2];
  40.     cpoint.x = -6;
  41.     cpoint.y = 0;
  42.     cpoint.z = 6;
  43.  
  44.     bcpoint.x = 6;
  45.     bcpoint.y = 0;
  46.     bcpoint.z = -6;
  47.  
  48.     for (surf=0;surf<=CONICPARTS;surf+=2) {
  49.         mAConic[surf] = rotpoint(cpoint,(((float)360)/CONICPARTS*surf));
  50.         mAConic[surf+1] = rotpoint(bcpoint,(((float)360)/CONICPARTS*surf));
  51.     }
  52. }
  53.  
  54. void TConic::CalcIntersect( const TPlane &plane ) {
  55.     float mu,temp;
  56.     int lineno,s;
  57.     p3d p1,p2,n,d;
  58.  
  59.     n = plane.mPNorm;
  60.     s = n.y*plane.mPlane[0].y + n.z*plane.mPlane[0].z;
  61.     mNumInt = 0;
  62.  
  63.     for(lineno=0;lineno<=CONICPARTS;lineno+=2) {
  64.         p1 = mAConic[lineno];
  65.         p2 = mAConic[lineno+1];
  66.         mConic[lineno] = p1;
  67.         mConic[lineno+1] = p2;
  68.         d.x = p1.x - p2.x;
  69.         d.y = p1.y - p2.y;
  70.         d.z = p1.z - p2.z;
  71.  
  72.         temp = n.y*d.y + n.z*d.z;
  73.         if (temp != 0) {
  74.             mu = (s - (n.x*p1.x + n.y*p1.y + n.z*p1.z))/temp;
  75.             //mu *= -1;
  76.             if ((mu >= -0.5) && (mu <= 0)) {
  77.                 mConic[lineno].x = p1.x + mu*d.x;
  78.                 mConic[lineno].y = p1.y + mu*d.y;
  79.                 mConic[lineno].z = p1.z + mu*d.z;
  80.                 mIntersect[mNumInt] = lineno;
  81.                 mNumInt++;
  82.             }
  83.             if ((mu <= -0.5) && (mu >= -1)) {
  84.                 mConic[lineno+1].x = p1.x + mu*d.x;
  85.                 mConic[lineno+1].y = p1.y + mu*d.y;
  86.                 mConic[lineno+1].z = p1.z + mu*d.z;
  87.                 mIntersect[mNumInt] = lineno + 1;
  88.                 mNumInt++;
  89.             }
  90.         }
  91.     }
  92. }
  93.  
  94. TPlane::TPlane():
  95.     mPAng(0)
  96. {
  97.     mAPlane = 0l;
  98.     mPlane = 0l;
  99.     mPDis = 2;
  100. }
  101.  
  102. TPlane::~TPlane()
  103. {
  104.     if( mAPlane )
  105.         delete mAPlane;
  106.     if( mPlane )
  107.         delete mPlane;
  108. }
  109.  
  110. void TPlane::Init()
  111. {
  112.     mAPlane = new p3d[4];
  113.     mPlane = new p3d[4];
  114.  
  115.  
  116.     mAPlane[0].x = -6; mAPlane[0].y = -6; mAPlane[0].z = mPDis;
  117.     mAPlane[1].x = 6; mAPlane[1].y = -6; mAPlane[1].z = mPDis;
  118.     mAPlane[2].x = 6; mAPlane[2].y = 6; mAPlane[2].z = mPDis;
  119.     mAPlane[3].x = -6; mAPlane[3].y = 6; mAPlane[3].z = mPDis;
  120. }
  121.  
  122. void TPlane::SetDistance( float dis )
  123. {
  124.     int surf;
  125.  
  126.     mPDis = dis;
  127.     if( mPDis < 0.25 )
  128.         mPDis = 0.25;
  129.     else if( mPDis > 7 )
  130.         mPDis = 7;
  131.     for( surf = 0; surf < 4; surf++ )
  132.         mAPlane[surf].z = mPDis;
  133.     RotPlane();
  134. }
  135.  
  136. void TPlane::RotPlane() {
  137.     int vert;
  138.  
  139.     for( vert=0; vert<4; vert++ ) {
  140.         mPlane[vert].x = mAPlane[vert].x;
  141.         mPlane[vert].y = GetCosAngle()*mAPlane[vert].y-GetSinAngle()*mAPlane[vert].z;
  142.         mPlane[vert].z = GetSinAngle()*mAPlane[vert].y+GetCosAngle()*mAPlane[vert].z;
  143.     }
  144.     CalcNormal();
  145. }
  146.  
  147. void TPlane::SetAngle( float ang ) {
  148.     mPAng.SetAngle( ang );
  149.     RotPlane();
  150. }
  151.  
  152. void TPlane::CalcNormal() {
  153.     p3d v1, v2, n;
  154.  
  155.     v1.x = mPlane[0].x - mPlane[1].x;
  156.     v1.y = mPlane[0].y - mPlane[1].y;
  157.     v1.z = mPlane[0].z - mPlane[1].z;
  158.  
  159.     v2.x = mPlane[0].x - mPlane[2].x;
  160.     v2.y = mPlane[0].y - mPlane[2].y;
  161.     v2.z = mPlane[0].z - mPlane[2].z;
  162.  
  163.     n.x = v1.y*v2.z - v1.z*v2.y;
  164.     n.y = v1.z*v2.x - v1.x*v2.z;
  165.     n.z = v1.x*v2.y - v1.y*v2.x;
  166.     mPNorm = n;
  167. }